//
//  NSString+THFrameSize.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "NSString+THFrameSize.h"

@implementation NSString (THFrameSize)

- (CGSize)getSizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
   CGSize resultSize = CGSizeZero;
   if (self.length <= 0) {
       return resultSize;
   }
   NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
   style.lineBreakMode = NSLineBreakByWordWrapping;
   resultSize = [self boundingRectWithSize:CGSizeMake(floor(size.width), floor(size.height))//用相对小的 width 去计算 height / 小 heigth 算 width
                                   options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin)
                                attributes:@{NSFontAttributeName: font,
                                             NSParagraphStyleAttributeName: style}
                                   context:nil].size;
   resultSize = CGSizeMake(floor(resultSize.width + 1), floor(resultSize.height + 1));//上面用的小 width（height） 来计算了，这里要 +1
   return resultSize;
}

- (CGFloat)getHeightWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
   return [self getSizeWithFont:font constrainedToSize:size].height;
}
- (CGFloat)getWidthWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
   return [self getSizeWithFont:font constrainedToSize:size].width;
}

- (CGSize)textSizeIn:(CGSize)size font:(UIFont *)font
{
   return [self textSizeIn:size font:font breakMode:NSLineBreakByWordWrapping];
}

- (CGSize)textSizeIn:(CGSize)size font:(UIFont *)afont breakMode:(NSLineBreakMode)breakMode
{
   return [self textSizeIn:size font:afont breakMode:NSLineBreakByWordWrapping align:NSTextAlignmentLeft];
}

- (CGSize)textSizeIn:(CGSize)size font:(UIFont *)afont breakMode:(NSLineBreakMode)abreakMode align:(NSTextAlignment)alignment
{
   NSLineBreakMode breakMode = abreakMode;
   UIFont *font = afont ? afont : [UIFont systemFontOfSize:17];
   
   CGSize contentSize = CGSizeZero;
   
   NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
   paragraphStyle.lineBreakMode = breakMode;
   paragraphStyle.alignment = alignment;
   
   NSDictionary* attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle};
   contentSize = [self boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:attributes context:nil].size;
   contentSize = CGSizeMake((int)contentSize.width + 1, (int)contentSize.height + 1);
   return contentSize;
}
#pragma mark - 计算文本
+ (CGSize)sizeWithString:(NSString *)string font:(UIFont *)font constrainedToWidth:(CGFloat)width{
   
   
   UIFont *textFont = font ? font : [UIFont systemFontOfSize:[UIFont systemFontSize]];
   
   CGSize textSize;
   
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 70000
   if ([string respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
   {
       NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
       paragraph.lineBreakMode = NSLineBreakByWordWrapping;
       NSDictionary *attributes = @{NSFontAttributeName: textFont,
                                    NSParagraphStyleAttributeName: paragraph};
       textSize = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                       options:(NSStringDrawingUsesLineFragmentOrigin |
                                                NSStringDrawingTruncatesLastVisibleLine)
                                    attributes:attributes
                                       context:nil].size;
   } else
   {
       textSize = [string sizeWithFont:textFont
                     constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
                         lineBreakMode:NSLineBreakByWordWrapping];
   }
#else
   NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
   paragraph.lineBreakMode = NSLineBreakByWordWrapping;
   NSDictionary *attributes = @{NSFontAttributeName: textFont,
                                NSParagraphStyleAttributeName: paragraph};
   textSize = [string boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                   options:(NSStringDrawingUsesLineFragmentOrigin |
                                            NSStringDrawingTruncatesLastVisibleLine)
                                attributes:attributes
                                   context:nil].size;
#endif
   
   return CGSizeMake(ceil(textSize.width), ceil(textSize.height));
   
}

@end
